$(function () {
    var shopId = getQueryString('shopId');
    var isEdit = shopId ? true : false;
    var initUrl = '/shopAdmin/getShopInitInfo';
    var registShopUrl = '/shopAdmin/registShop';
    var shopInfoUrl = '/shopAdmin/getShopById?shopId='+shopId;
    var editShopUrl = '/shopAdmin/modifyShop';
    if (!isEdit){
        getShopInitInfo()
    }else{
        getShopInfo(shopId);
    }

    // alert(registShopUrl);
    // alert('进行到这了');
    // getShopInitInfo();

    function getShopInfo(shopId) {
        $.getJSON(shopInfoUrl, function(data) {
            if (data.success) {
                var shop = data.shop;
                $('#shop_name').val(shop.shopName);
                $('#shop_addr').val(shop.shopAddr);
                $('#shop_phone').val(shop.phone);
                $('#shop_desc').val(shop.shopDesc);
                var shopCategory = '<option data-id="'
                    + shop.shopCategory.shopCategoryId + '" selected>'
                    + shop.shopCategory.shopCategoryName + '</option>';
                var tempAreaHtml = '';
                data.areaList.map(function(item, index) {
                    tempAreaHtml += '<option data-id="' + item.areaId + '">'
                        + item.areaName + '</option>';
                });
                $('#shop_category').html(shopCategory);
                $('#shop_category').attr('disabled','disabled');
                $('#area').html(tempAreaHtml);
                // $('#area').attr('data-id',shop.areaId);
                $("#area option[data-id='"+shop.area.areaId+"']").attr("selected","selected");
            }
        });
    }

    // alert("d到这个方法了吗1")
    function getShopInitInfo() {
        // alert("d到这个方法了吗11")
        $.getJSON(initUrl, function (data) {
            // alert("d到这个方法了吗13");
            if (data.success) {
                var tempHtml = '';
                var tempAreaHtml = '';
                data.shopCategoryList.map(function (item, index) {
                    tempHtml += '<option data-id="' + item.shopCategoryId
                        + '">' + item.shopCategoryName + '</option>';
                    // alert("d到这个方法了吗12");
                });
                data.areaList.map(function (item, index) {
                    tempAreaHtml += '<option data-id="' + item.areaId
                        + '">' + item.areaName + '</option>';
                    // alert("d到这个方法了吗15");
                });
                $('#shop_category').html(tempHtml);
                $('#area').html(tempAreaHtml);
                // alert("d到这个方法了吗14");
            }
        });
    }
        $('#submit').click(function () {
            var shop = {};
            if (isEdit){
                shop.shopId=shopId;
            }
            shop.shopName = $('#shop_name').val();
            shop.shopAddr = $('#shop_addr').val();
            shop.phone = $('#shop_phone').val();
            shop.shopDesc = $('#shop_desc').val();
            shop.shopCategory = {
                shopCategoryId: $('#shop_category').find('option').not(function () {
                    return !this.selected;
                }).data('id')
            };
            shop.area = {
                areaId: $('#area').find('option').not(function () {
                    return !this.selected;
                }).data('id')
            };
            var shopImg = $('#shop_img')[0].files[0];
            var formData = new FormData();
            formData.append('shopImg', shopImg);
            formData.append('shopStr', JSON.stringify(shop));
            var verifyCodeActual = $('#shop_captcha').val();
            if (!verifyCodeActual) {
                $.toast('请输入验证码！');
                return;
            }
            formData.append('verifyCodeActual', verifyCodeActual);
            $.ajax({
                url:(isEdit?editShopUrl:registShopUrl),
                type: 'POST',
                data: formData,
                contentType: false,
                processData: false,
                cache: false,
                success: function (data) {
                    // alert("执行reshop方法了吗");
                    if (data.success) {
                        $.toast('提交成功!');
                    } else {
                        $.toast('提交失败' + data.errMsg);
                    }
                    /*提交成功与否都会刷新验证码*/
                    $('#captcha_img').click();
                }
            });
        });
});